package controller;


import pojo.Result;
import service.GenerateService;
import service.GenerateServiceImpl;

import java.io.File;

/**
 * @author tms
 * @date 2023/9/25
 */
public class CheckController {
    private static final GenerateService generateService = new GenerateServiceImpl();

    /**
     * 题目文件
     */
    private File exercisesFile;
    /**
     * 答案文件
     */
    private File answerFile;

    /**
     * 读入题目文件
     */
    private void selectExercisesFile(String exercisefile) {
        exercisesFile = new File(exercisefile);
        if (exercisesFile == null) {
            System.out.println("题目文件为空！");
        }
    }

    /**
     * 读入答案文件
     */
    private void selectAnswerFile(String answerfile) {
        answerFile = new File(answerfile);
        if (answerFile == null) {
            System.out.println("答案文件为空！");
        }
    }

    /**
     * 控制台打印正确率
     *
     * @param result 结果
     */
    private void setCorrectAndWrongLabel(Result result) {
        System.out.println("正确题数：" + result.getCorrectList().size());
        System.out.println("错误题数：" + result.getWrongList().size());
    }

    /**
     * 对给定的题目文件和答案文件，判定答案中的对错并进行数量统计
     */
    public void checkPaper(String exercisefile,String answerfile) {
        try {
            selectExercisesFile(exercisefile);
            selectAnswerFile(answerfile);
            Result result = generateService.checkAnswer(generateService.readFile(exercisesFile, answerFile));
            //控制台打印正确率结果信息
            setCorrectAndWrongLabel(result);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
